<?php

namespace api\modules\v1\controllers;
use api\common\controllers\UserController as U;
use api\modules\v1\models\Authentication;
use api\modules\v1\models\User;
use yii\web\ServerErrorHttpException;
use yii\web\HttpException;
use api\modules\v1\models\Code;
use Yii;

class AuthenticationController extends U
{
    public $modelClass = 'api\modules\v1\models\Authentication';

    public function actions()
    {
        $actions = parent::actions();
        unset($actions['index'],$actions['update'], $actions['create'], $actions['delete'], $actions['view']); //卸载操作
        return $actions;
    }

    /*
     * 实名认证申请提交
     */
    public function actionCreate()
    {
        $model = new $this->modelClass();
        $model->attributes = Yii::$app->request->post();
        $model->userid = Yii::$app->user->identity->id;
        $model->time = date('Y-m-d H:i:s');
        if($model->save()){
            User::updateAll(['is_authentication'=>2],['id'=>Yii::$app->user->identity->id]);
            return [
                "message"=>"提交成功!请等待审核",
            ];
        }elseif (!$model->hasErrors()) {
            throw new ServerErrorHttpException('由于不明原因未能创建对象.');
        }else{
            throw new HttpException(401,array_values($model->getFirstErrors())[0],0);
        }
    }
}